﻿using System;
using System.Collections.Generic;
using System.Linq;
using Ross.EntityFramework;
using Ross.EntityFramework.Model;
using System.Text;
using System.Threading.Tasks;
using AutoMapper;
using System.Linq.Expressions;

namespace Ross.Service
{
    public class ExamDBService : BaseService, IDisposable
    {
        private TranDbContext dbContext;
        public IRepository<ExamDB, int> repository;
        public ExamDBService(string nameOrConnectionString)
        {
            dbContext = new TranDbContext(nameOrConnectionString);
            repository = new Repository<ExamDB, int>(dbContext);
        }
        public void Dispose()
        {
            dbContext.Dispose();
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="examDB"></param>
        /// <returns></returns>
        public ResultDto<bool> Create(Dto.ExamDB examDB)
        {
            ResultDto<bool> result = new ResultDto<bool>();
            try
            {
                var obj = repository.Insert(Mapper.Map<ExamDB>(examDB));
                result.status = 200;
                result.datas = true;
            }
            catch (Exception ex)
            {
                result.message = ex.Message;
                result.datas = false;
            }
            return result;
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ResultDto<bool> Delete(int id)
        {
            ResultDto<bool> result = new ResultDto<bool>();
            try
            {
                repository.Delete(id);
                result.status = 200;
                result.datas = true;
            }
            catch (Exception ex)
            {
                result.message = ex.Message;
                result.status = 500;
                result.datas = false;
            }
            return result;
        }
        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="examDB"></param>
        /// <returns></returns>
        public ResultDto<bool> Update(Dto.ExamDB examDB)
        {
            ResultDto<bool> result = new ResultDto<bool>();
            try
            {
                repository.InsertOrUpdate(Mapper.Map<ExamDB>(examDB));
                result.status = 200;
                result.datas = true;
            }
            catch (Exception ex)
            {
                result.message = ex.ToString();
                result.status = 500;
                result.datas = false;
            }
            return result;
        }
        /// <summary>
        /// 获取单页
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ResultDto<Dto.ExamDB> GetByID(int id)
        {
            ResultDto<Dto.ExamDB> result = new ResultDto<Dto.ExamDB>();
            var obj = repository.Get(id);
            if (obj != null)
            {
                result.status = 200;
                result.datas = Mapper.Map<Dto.ExamDB>(obj);
            }
            else
            {
                result.message = "id is not exist";
                result.status = 500;
            }
            return result;
        }
        /// <summary>
        /// 获取列表
        /// </summary>
        /// <returns></returns>
        public ResultDto<List<Dto.ExamDB>> GetAllList()
        {
            ResultDto<List<Dto.ExamDB>> result = new ResultDto<List<Dto.ExamDB>>();
            var lists = repository.GetAllList();
            result.datas = Mapper.Map<List<Dto.ExamDB>>(lists);
            return result;
        }
        /// <summary>
        /// 分页获取列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="keywords"></param>
        /// <returns></returns>
        public ResultDto<List<Dto.ExamDB>> GetPageList(int pageIndex = 1, int pageSize = 20, string keywords = "")
        {
            ResultDto<List<Dto.ExamDB>> result = new ResultDto<List<Dto.ExamDB>>();
            Expression<Func<ExamDB, bool>> exp = o => o.ID > 0;
            if (!string.IsNullOrEmpty(keywords))
                exp = exp.ExpressionAnd(t => t.ExamTitle.Contains(keywords));
            var lists = repository.GetPageList(pageIndex, pageSize, exp);
            result.datas = Mapper.Map<List<Dto.ExamDB>>(lists.datas);
            return result;
        }
        /// <summary>
        /// 获取所有分类
        /// </summary>
        /// <returns></returns>
        public ResultDto<List<string>> GetClassName()
        {
            ResultDto<List<string>> result = new ResultDto<List<string>>();
            var lists = repository.GetAllList().Select(o=>o.ClassName).Distinct().ToList();
            result.datas = lists;
            return result;
        }
    }
}
